package com.moming.service;

import com.moming.mapper.UserMapper;
import com.moming.pojo.User;
import com.moming.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserService {
    //通过工具类获取工厂对象
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public User login(String username,String password){
        //获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用dao层
        User user = mapper.selectUser(username, password);
        sqlSession.close();
        return user;
    }
    public boolean register(User user){
        //获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用dao层
        User user1 = mapper.selectUserName(user.getUsername());
        //判断用户是否已存在,不存在就可以注册
        if(user1==null){
            mapper.insertUser(user);
            sqlSession.commit();
            sqlSession.close();
        }
        return user1==null;
    }
}